// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1

Rectangle {
    id: page //unique id of the main object
    width: 640 //width of the window
    height: 640 //height of the window
    color: "black" //background color of the window
    property int filterOn: 0 //is filter turned on
    property int spectrogramOn: 0 //is spectrogram turned on
    property double alphaPower: 0 //current alpha power
    property int packetCounter: 0 //used for visualizing activity

    signal turnFilterOn(int fbandLow, int fbandHigh, int order); //signal for turning filter on
    signal turnFilterOff(); //signal for turning filter off

    signal turnSpectrogramOn(int samples, int length, int delta); //signal for turning spectrogram on
    signal turnSpectrogramOff(); //signal for turning spectrogram off
    /**
      Function reciving channel name and list of power values.
      It contains the whole logic of power calculation and storing.
      */
    function spectrogramValue(channel, valuelist)
    {
	page.alphaPower = 0;
	for (var v in valuelist)
	    page.alphaPower += parseFloat(valuelist[v]);
    }

    /**
      Function receiving time ticks from C++ code.
      */
    function timeTick()
    {
	page.packetCounter = (page.packetCounter + 1)%16;
    }

    Text
    {
	anchors.centerIn: parent
	font.pixelSize: 60
	color: "white";
	text: page.alphaPower
    }

    Rectangle
    {
	//quit button
	color: "red"
	anchors.right: parent.right
	anchors.top: parent.top
	width: 50
	height: 50
	MouseArea
	{
	    anchors.fill: parent
	    onClicked:
	    {
		Qt.quit();
	    }
	}
    }

    //grid with buttons
    Grid
    {
	id: controlsGrid
	Rectangle
	{
	    //filtering button
	    color: {if (filterOn) return "red"; return "blue";}
	    width: 100
	    height: 33
	    Text {
		anchors.centerIn: parent
		text: {if (filterOn) return "filter on"; return "filter off";}
		color: "white"
	    }
	    MouseArea
	    {
		anchors.fill: parent
		onClicked:
		{
		    //if filter is currently on, turn it off
		    if (page.filterOn)
		    {
			page.filterOn = 0;
			page.turnFilterOff();
		    }
		    //filter is off, turn it on
		    else
		    {
			page.filterOn = 1;
			//8-13 Hz, 32 samples window
			page.turnFilterOn(8,13,32);
		    }
		}
	    }
	}
	Rectangle
	{
	    //spectrogram button
	    color: {if (spectrogramOn) return "red"; return "blue";}
	    width: 100
	    height: 33
	    Text {
		anchors.centerIn: parent
		text: {if (spectrogramOn) return "spec on"; return "spec off";}
		color: "white"
	    }
	    MouseArea
	    {
		anchors.fill: parent
		onClicked:
		{
		    if (page.spectrogramOn)
		    {
			page.spectrogramOn = 0;
			page.turnSpectrogramOff();
		    }
		    else
		    {
			page.spectrogramOn = 1;
			//turn spectrogram on, 128Hz sampling frequency, window size of 128 samples, performed every 16 samples (8 times/sec)
			page.turnSpectrogramOn(128,128,16);
		    }
		}
	    }
	}

	Rectangle
	{
	    //activity visualization
	    color: "red"
	    height: 33
	    width: height
	    opacity: page.packetCounter/16.0
	}

    }

}
